import {
  importData,
  getByPage,
  add,
  update,
  deleteById,
  getById,
} from '@/services/forecastScheduler';

export default {
  namespace: 'forecastScheduler',
  state: {
    data: {},
  },
  effects: {
    *fetch({ payload, callback }, { call }) {
      const resp = yield call(getByPage, payload);
      const { state, data = [] } = resp;
      if (resp && state === 0 && callback) {
        callback(data);
      }
    },
    *importData({ payload, callback }, { call }) {
      const { state, data } = yield call(importData, payload);
      if (state === 0 && callback) {
        callback(data);
      }
    },
    *add({ payload, callback }, { call }) {
      const resp = yield call(add, payload);
      if (callback) {
        callback(resp);
      }
    },

    *update({ payload, callback }, { call }) {
      const resp = yield call(update, payload);
      if (callback) {
        callback(resp);
      }
    },

    *deleteById({ payload, callback }, { call }) {
      const resp = yield call(deleteById, payload);
      const { state, data } = resp;
      if (state === 0 && callback) {
        callback(data);
      }
    },

    *getById({ payload, callback }, { call }) {
      const resp = yield call(getById, payload);
      const { state, data } = resp;
      if (resp && state === 0 && callback) {
        callback(data);
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
  },
};
